/*************************************************************
 * Title: SecurityHelper.java
 * Description: 
 * Author: Huang Shaobin
 * Email: shaobin.software@gmail.com shaobin_software@163.com
 * CreateTime: Feb 1, 2013 11:39:13 AM
 * Copyright © 落羽社区 All right reserved
 ************************************************************/
package snow.core.security;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

/**
 *
 */
public class SecurityHelper {

	private static final Set<Integer> OPERATIONS;

	static {
		final OperationType[] types = OperationType.values();
		final Set<Integer> tempSet = new HashSet<Integer>(types == null ? 10 : types.length);
		if (!ArrayUtils.isEmpty(types)) {
			for (OperationType type : types) {
				tempSet.add(Integer.valueOf(type == null ? 0 : type.getValue()));
			}
			tempSet.remove(Integer.valueOf(0));
		}
		OPERATIONS = Collections.unmodifiableSet(tempSet);
	}

	/**
	 * 计算授权权限值
	 * 
	 * @param operations
	 * @return
	 */
	public static int authorize(int... operations) {
		int accessVal = 0;
		if (!ArrayUtils.isEmpty(operations)) {
			final Set<Integer> operationSet = new HashSet<Integer>(OPERATIONS.size());
			for (final int operation : operations) {
				if (OPERATIONS.contains(Integer.valueOf(operation))) {
					operationSet.add(Integer.valueOf(operation));
				}
			}
			for (Integer operation : operationSet) {
				accessVal += (operation == null ? 0 : operation.intValue());
			}
		}
		return accessVal;
	}

	/**
	 * 通过目标方法获取权限值
	 * 
	 * @param method
	 * @return
	 */
	public static int getOperationFrom(Method method) {
		final Operation operation = method == null ? null : method.getAnnotation(Operation.class);
		final OperationType operationType = operation == null ? null : operation.value();
		return operationType == null ? 0 : operationType.getValue();
	}

	/**
	 * 通过目标对象及目标方法获取资源信息
	 * 
	 * @param target
	 *            目标对象
	 * @param method
	 *            目标方法
	 * @return
	 */
	public static String getResourceUriFrom(Object target, Method method) {
		String uri = null; // 临时变量
		final StringBuilder resourceUri = new StringBuilder(255);
		final Class<?> targetClass = target == null ? null : target.getClass();
		final Package pack = targetClass == null ? null : targetClass.getPackage();
		final Resource packResource = pack == null ? null : pack.getAnnotation(Resource.class);
		resourceUri.append(packResource == null ? "" : StringUtils.isBlank((uri = packResource.uri())) ? "" : uri);
		final Resource classResource = targetClass == null ? null : targetClass.getAnnotation(Resource.class);
		resourceUri.append(classResource == null ? "" : StringUtils.isBlank((uri = classResource.uri())) ? "" : uri);
		final Resource methodResource = method == null ? null : method.getAnnotation(Resource.class);
		resourceUri.append(methodResource == null ? "" : StringUtils.isBlank((uri = methodResource.uri())) ? "" : uri);
		return resourceUri.toString();
	}

	/**
	 * 判断访问人是否持有相应权限
	 * 
	 * @param accessor
	 * @param operation
	 * @return
	 */
	public static boolean hasPermission(int accessor, int operation) {
		return (accessor & operation) == operation;
	}

}
